package com.zhan.ielts.dao.impl;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import com.zhan.frame.core.dao.impl.BaseDaoImpl;
import com.zhan.frame.core.exception.DaoException;
import com.zhan.ielts.dao.AudioAnswerDao;
import com.zhan.ielts.domain.AudioAnswer;

@Repository
public class AudioAnswerDaoImpl extends BaseDaoImpl<AudioAnswer> implements AudioAnswerDao {

	@Override
	public void saveOrUpdate(AudioAnswer entity) {
		try {
			if (entity != null) {
				if (StringUtils.isBlank(entity.getId()))
					entity.setId(generateId());
				sqlSessionTemplate.insert(getSqlName("saveOrUpdate"), entity);
			}
		} catch (Exception e) {
			throw new DaoException(String.format("添加对象出错！语句：%s", getSqlName("saveOrUpdate")), e);
		}

	}

	@Override
	public List<AudioAnswer> selectLatest(AudioAnswer entity) {
		return sqlSessionTemplate.selectList(getSqlName("selectLatestOnes"), entity);

	}

	@Override
	public Long getCountByCutom(AudioAnswer entity) {
		return sqlSessionTemplate.selectOne(getSqlName("selectLatestCount"), entity);

	}

	@Override
	public void preSaveHandle(AudioAnswer entity) {
		try {
			if (entity != null) {
//				if (StringUtils.isEmpty(entity.getPracticePad()))
//					sqlSessionTemplate.update(getSqlName("preSaveHandlePad"), entity);
//
//				if (StringUtils.isEmpty(entity.getPracticePhone()))
//					sqlSessionTemplate.update(getSqlName("preSaveHandlePhone"), entity);
			}
		} catch (Exception e) {
			throw new DaoException(String.format("添加对象出错！语句：%s", getSqlName("preSaveHandle")), e);
		}
	}

	@Override
	public void updateByCustom(AudioAnswer entity) {
		try {
			if (entity != null) {
				if (StringUtils.isBlank(entity.getId()))
					entity.setId(generateId());
				sqlSessionTemplate.update(getSqlName("updateByCustom"), entity);
			}
		} catch (Exception e) {
			throw new DaoException(String.format("添加对象出错！语句：%s", getSqlName("updateByCustom")), e);
		}
	}

}
